package org.simple.test.jdbc

import com.alibaba.druid.pool.DruidDataSource
import org.junit.Test
import org.nutz.dao.ConnCallback
import org.nutz.dao.impl.NutDao
import org.nutz.dao.sql.Sql
import org.nutz.dao.sql.SqlCallback

import java.sql.Connection
import java.sql.ResultSet
import java.sql.ResultSetMetaData
import java.sql.SQLException
import java.sql.Statement

class JdbcTest {
    @Test
    void testExecuteQuery() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/CUWMM?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;autoReconnectForPools=true");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("1234");
        druidDataSource.setInitialSize(10);
        druidDataSource.setMaxActive(100);
        druidDataSource.setMinIdle(10);
        druidDataSource.setDefaultAutoCommit(false);
        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setFilters("stat");
        druidDataSource.setValidationQuery("select 1");

        Connection conn = druidDataSource.getConnection();

        try {
            Statement st = conn.createStatement();
            String sql = "select * from status_item where status_type_id='ORDER_STATUS'";
            ResultSet rs = st.executeQuery(sql);

            ResultSetMetaData resultSetMetaData = rs.getMetaData();
            int columnCount = resultSetMetaData .getColumnCount();
            String columnNames = "";
            for(int i = 1; i <=columnCount; i++) {
                columnNames += resultSetMetaData.getColumnName(i) + "      ";
            }
            println(columnNames);
            while(rs.next()) {
                for(int i = 1; i <= columnCount; i++) {
                    print rs.getString(i) + "     ";
                }
                print "\n"
            }

            st.close();
            rs.close();
        } catch (Exception e) {
            throw new Exception(e.message);
        } finally {
            conn.close();
        }


//        TestDao testDao = new TestDao();
//        testDao.setDataSource(druidDataSource);
//
//        NutDao dao = testDao.getDao();
//        dao.run(new ConnCallback() {
//            @Override
//            void invoke(Connection conn) throws Exception {
//
//            }
//        });
    }
}
